<?php
namespace Qgs\Controller;

/**
 * 快捷菜单
 * Class ShortcutMenuController
 * @package Qgs\Controller
 */
class ShortcutMenuController extends BaseController {

    public function index(){

        $m = M('store_shortcut_menu');
        $local_id = session('userInfo')['local_id'];
        $shortMenu = $m->where(['local_id' => $local_id])->order(['order' => 'asc'])->select();

        $this->assign(['shortMenu' => $shortMenu]);
        $this->display();
    }

    /**
     * 新增自定义快捷菜单
     */
    public function add(){
        if ($_POST) {
            $name = I('name', '', htmlspecialchars());
            if (empty($name)) {
                $this->error('名称填写不正确');
            }
            $url = I('url', '', htmlspecialchars());
            $img = I('img', '', htmlspecialchars());
            $m = M('store_shortcut_menu');
            $order = I('order', '', htmlspecialchars());
            $type = I('type', '', htmlspecialchars());
            $data = [
                'local_id' => session('userInfo')['local_id'],
                'name' => $name,
                'img' => $img,
                'url' => $url,
                'type' => $type,
                'order' => $order

            ];
            $m->add($data);
            get_curl(session('userInfo')['local_id']);
            $this->success('完成！', '/Qgs/ShortcutMenu/index');exit;
        }

        $this->display();
    }

    public function edit(){
        $id = I('id', '', htmlspecialchars());
        $m = M('store_shortcut_menu');
        $shortMenu = $m->where(['id' => $id])->find();

        if ($_POST) {
            $name = I('name', '', htmlspecialchars());
            $url = trim(I('url', '', htmlspecialchars()));
            $img = I('img', '', htmlspecialchars());
            $order = I('order', '', htmlspecialchars());
            $type = I('type', '', htmlspecialchars());

            if (empty($name)) {
                $this->error('名称填写不正确');
            }
            if (empty($img)) {
                $img = $shortMenu['img'];
            }

            $data = [
                'name' => $name,
                'img' => $img,
                'url' => $url,
                'order' => $order,
                'type' => $type,
            ];
            $menu = $m->where(['id' => $id, 'local_id' => session('userInfo')['local_id']])->save($data);
            get_curl(session('userInfo')['local_id']);
            if ($menu !== false) {
                $this->success('完成！', '/Qgs/ShortcutMenu/index');exit;
            }else{
                $this->error('失败！');exit;
            }
        }

        $this->assign(['shortMenu' => $shortMenu]);
        $this->display();
    }

    public function delete(){
        $id = I('id', '', htmlspecialchars());
        $m = M('store_shortcut_menu');
        $m->delete($id);
        get_curl(session('userInfo')['local_id']);
        $this->success('完成！', '/Qgs/ShortcutMenu/index');
    }

    /**
     * 产品增加
     */
    public function productAdd(){

        $name = I('name', '', htmlspecialchars());
        $id = I('id', '', htmlspecialchars());

        if (!empty($name)) {
            $where['b.name'] = array('like',"%$name%");
        }

        $page = max(I('get.p'), 1);
        $num = max(I('get.num'), 20);

        $order = "a.store_pid desc";
        $product_relation_model = M('b2c_store_product a');

        $where['a.local_id'] = session('userInfo')['local_id'];
        $where['a.store_status'] = 'true';
        $where['b.disabled'] = 'false';

        $field = "a.marketprice,a.price,a.store,a.discount,a.store_pid,b.product_id,b.goods_id,b.title,b.name,b.spec_info";

        // 可售货品
        $count = $product_relation_model->join(C('DB_PREFIX') . 'b2c_products b on a.product_id=b.product_id')
            ->where($where)
            ->count();

        $Page = new \Think\Page($count, $num);
        $show = $Page->show();
        $product_relation_msg = $product_relation_model->join(C('DB_PREFIX') . 'b2c_products b on a.product_id=b.product_id', 'left')
            ->where($where)
            ->order($order)
            ->field($field)
            ->page($page, $num)
            ->select();
//echo $product_relation_model->_sql();
        $menu = M('store_shortcut_menu')->where(['id' => $id, 'local_id' => session('userInfo')['local_id']])->find();
        $value = unserialize($menu['value']);

        $this->assign([
            'product_relation_msg' => $product_relation_msg,
            'show' => $show,
            'id' => $id,
            'value' => $value
        ]);

        $this->display();
    }

    /**
     * 查看已选产品列表
     */
    public function productAddExist(){
        $name = I('name', '', htmlspecialchars());
        $id = I('id', '', htmlspecialchars());

        if (!empty($name)) {
            $where['b.name'] = array('like',"%$name%");
        }

        $menu = M('store_shortcut_menu')->where(['id' => $id, 'local_id' => session('userInfo')['local_id']])->find();
        $value = unserialize($menu['value']);
        $value_string = implode(',', $value);

        $page = max(I('get.p'), 1);
        $num = max(I('get.num'), 20);

        $order = "a.store_pid desc";
        $product_relation_model = M('b2c_store_product a');

        $where['a.local_id'] = session('userInfo')['local_id'];
        $where['a.store_status'] = 'true';
        $where['b.disabled'] = 'false';
        $where['a.store_pid'] = ['in', $value_string];

        $field = "a.marketprice,a.price,a.store,a.discount,a.store_pid,b.product_id,b.goods_id,b.title,b.name,b.spec_info";

        // 可售货品
        $count = $product_relation_model->join(C('DB_PREFIX') . 'b2c_products b on a.product_id=b.product_id')
            ->where($where)
            ->count();

        $Page = new \Think\Page($count, $num);
        $show = $Page->show();
        $product_relation_msg = $product_relation_model->join(C('DB_PREFIX') . 'b2c_products b on a.product_id=b.product_id', 'left')
            ->where($where)
            ->order($order)
            ->field($field)
            ->page($page, $num)
            ->select();


        $this->assign([
            'product_relation_msg' => $product_relation_msg,
            'show' => $show,
            'id' => $id,
            'value' => $value
        ]);

        $this->display();
    }

    /**
     * 保存产品列表
     */
    public function productSave(){

        $m = M('store_shortcut_menu');
        $id = I('get.id', '', htmlspecialchars());

        if ($_POST) {
            get_curl(session('userInfo')['local_id']);
            $data = I('data', '', htmlspecialchars());
            $value = [];
            $menu = $m->where(['id' => $id])->find();
            if (!empty($menu['value'])) { // 如果value有值
                $value = unserialize($menu['value']);
            }
            if (!empty($data)) {
                foreach ($data as $v) {
                    $arr = explode('-', $v);

                    if ($arr[1] == 0) { // 第二个元素为0删除
                        $key = array_search($arr[0], $value);
                        if ($key !== false) {
                            array_splice($value, $key, 1);
                        }
                    }else {
                        $key = array_search($arr[0], $value);
                        if ($key === false) {
                            array_push($value,$arr[0]);
                        }
                    }
                }

                $value = serialize($value);
                $m->where(['id' => $id])->save(['value' => $value]);
//                echo $m->_sql();
            }
        }
    }

    /**
     * 产品详情
     */
    public function productDetail(){
        $name = I('name', '', htmlspecialchars());
        $id = I('id', '', htmlspecialchars());

        if (!empty($name)) {
            $where['b.name'] = array('like',"%$name%");
        }

        $page = max(I('get.p'), 1);
        $num = max(I('get.num'), 20);

        $order = "a.store_pid desc";
        $product_relation_model = M('b2c_store_product a');

        $where['a.local_id'] = session('userInfo')['local_id'];
        $where['a.store_status'] = 'true';
        $where['b.disabled'] = 'false';

        $field = "a.marketprice,a.price,a.store,a.discount,a.store_pid,b.product_id,b.goods_id,b.title,b.name,b.spec_info";

        // 可售货品
        $count = $product_relation_model->join(C('DB_PREFIX') . 'b2c_products b on a.product_id=b.product_id')
            ->where($where)
            ->count();

        $Page = new \Think\Page($count, $num);
        $show = $Page->show();
        $product_relation_msg = $product_relation_model->join(C('DB_PREFIX') . 'b2c_products b on a.product_id=b.product_id', 'left')
            ->where($where)
            ->order($order)
            ->field($field)
            ->page($page, $num)
            ->select();

        $menu = M('store_shortcut_menu')->where(['id' => $id])->find();
        $value = $menu['value'];

        $this->assign([
            'product_relation_msg' => $product_relation_msg,
            'show' => $show,
            'id' => $id,
            'value' => $value
        ]);

        $this->display();
    }

    /**
     * 保存产品详情
     */
    public function productDetailAdd(){
        $id = I('get.id', '', htmlspecialchars());
        $item = I('post.item', '', htmlspecialchars());

        M('store_shortcut_menu')->where(['id' => $id])->save(['value' => $item]);
        get_curl(session('userInfo')['local_id']);
    }


}